Method of implementing fixed-width data types

ABSTRACT

A method of implementing fixed-width data types which are not directly supported by a programming language, in a portable manner. A set U of required fixed-width data types, an ordered set B of basic data types and a set V with all possible data type widths are provided as inputs ( 201 ). A generic data type G having an integer parameter and a data type parameter is created ( 202 ). For every data type width WVj from set V and basic data type Bi from set B a specialized data type G is created ( 203 ). Then, each required data type Uk from set U of required data types is implemented by creating and mapping required data type Uk to the implementation of Uk provided by the generic data type G 11  having the parameters data type width WB 1  and basic data type B 1  ( 204 ).

FIELD OF THE INVENTION

The invention relates to a method of implementing, in a portable manner,fixed width data types which are not directly supported by a programminglanguage.

BACKGROUND OF THE INVENTION

Different programming languages directly support different basic datatypes. Many programming languages allow additional user defined datatypes created out of basic data types. For example, arrays, structuresand records.

Data objects or variables have to be represented using a finite statemachine such as computer memory, which is a sequence of binary digits.Therefore integer data types supported by programming languages oftenhave a finite range. Programming languages often define the range of aparticular data type as the number of binary digits or bits that shouldbe used to represent a value belonging to that data type. This range ofa data type is represented as data type width. For example data type“short” in the Java programming language is defined to have a data typewidth of 16 bits giving a range of values −65536, −65535 . . . 0 . . .+65535.

Some programming languages do not define the data type width for basicdata types, and chose to leave the decision of defining these tocompiler implementors. This means that different compilers may considerdifferent data type width for the same data type. For example, inprogramming languages C and C++ valid data widths for the ‘int’ includes16-bits, 32-bits or 64-bits. Such data types therefore may havedifferent widths depending on the compiler used.

Complex programs developed using high level programming languages areoften organized into smaller units as given below.

-   -   a) Functions or procedures with a defined set of formal        parameters such as a list of data types. When invoked with a set        of actual arguments that is a list of matching data values or        objects, these functions perform some set of operations on those        values and/or objects possibly invoking other functions to        complete the operations.    -   b) User defined data types that define more complex data types        using other existing basic or user defined data types.

Often, the same operation such as swap or sorting may be defined fordifferent data types. Without generic programming, the programmer isforced to develop different functions for implementing the sameoperation for formal parameters with different data types. For example asort function for an array of integers and another for an array offloating point numbers.

Similarly, a complex data type such as a linked list or complex numbercan be defined using different basic data types. For example, one userdefined data type that can be used for integer real and imaginary partsand another for floating point real and imaginary parts. Genericprogramming allows the development of functions and complex data typesthat can be used with more than one set of formal parameter types orconstant data values. For example a single sort function for an array ofintegers, an array of floating point numbers or an array of any datatype that defines an ordering relationship such as “less than” betweenany two values in the domain of the data type. Different languages andcompilers may implement generic programming in different ways. The twocommon ways are static resolution of type parameters and dynamicresolution of type parameters. A generic function or type provides animplementation of the function or type, which can be used with differentsets of the type parameters. However, a programmer may sometime chooseto over-ride the implementation of the generic function or type, for aspecific set of type parameters. This is referred to as specialization,and such types or functions are referred to as specialized type forfunction.

An example in C++ is given below: / / Generic function swap.Template<class C>void swap (C & c1, C&c2) { C tmp; tmp = c1; c1 = c2; c2= tmp; } // specialized function swap for swapping 2 strings. // willonly work when length of strings is equal. // swaps string , instead ofswapping pointers to strings. template <> void swap (char * C1, char *c2) { char tmp; While (*c1 && *c2) { tmp = *c1; *c1 = *c2; *c2 = tmp; }/* while */ }

OBJECT OF THE INVENTION

Most programs are written to solve real world computational problems.Often, these programs have to represent data values from real world suchas dates, age, scientific values etc. using programming languages. Oftenprograms use objects of a particular data type to represent data values,to ensure representation of all possible values that can be assigned tothe object in real world using the data type. This requires thatprograms use fixed-width data types and not variable-width data types.However, where the programming language does not provide fixed-widthdata types the program developer is required to define a user definedfixed-width data type that uses one or more ‘appropriate’ basic datatype of the required width depending on the compiler used.

A program may use the following solutions to solve the problem ofimplementing fixed-width data types. Use a pre-processor to map macronames used as fixed-width data types to appropriate basic data type,depending on compiler and central processor unit.

Simple Example in C++:

-   -   #ifdef COMPILER_IS_VISUAL_C    -   #define int32 int    -   #elif COMPILER_IS_MSC    -   #define int32 long    -   #endif    -   int32 var1, var2 /* int32 will be int with VISUAL C++ */        -   /* int32 will be long with MSC */ . . .

The disadvantage of this method is that this process has to be repeatedfor each data type and compiler implementation and the program may needmodifications for porting to a new compiler.

In general preprocessor macros are not visible to the compiler and otherprogram development and troubleshooting tools such as debuggers andtherefore, the program is more difficult to maintain. This method cannotbe used if there is no basic data type with data type width matchingthat of the required fixed-width data type.

Another method is to use a pre-processor, and use an appropriatelanguage mechanism to create a fixed-width data type to appropriatebasic data type, depending on compiler and the central processor unit.

Simple Example in C++: #ifdef COMPLIER_IS_VISUAL_C typedef int int32;#elif COMPILER_IS_MSC typedef long int32; #endif

-   -   int32 var1, var2 /* int32 will be int with VISUAL C++ */    -   /* int32 will be long with MSC */

The disadvantage in this method is also that this process is repeatedfor each data type and compiler implementation. The program may needmodifications for porting to a new compiler. This method also cannot beused if there is no basic data type with data type width matching thatof the required fixed-width data type.

Still another method is to define for each fixed-width data type a newuser-defined data type. This new type uses an array or structure ofbasic data types, along with programmer supplied functions thatimplement the necessary operations defined for the data type.

Example in C ++: Struct int16 { // comment “ use array of 2 int8's torepresent one int16 int8 v {2}; // additional structure members todefine operations. friend int16 operator + (int16, int16); friend int 16operator − (int16, int 16); }; struct int 32 { // comment: use array of2 int16's to represent one int32 int16 v [2]; // additional structuremembers to define operations. friend int32 operator +{int32, int32);friend int 32 operator − (int32, int32); };

The disadvantage is that user defined data types are often lessefficient than basic data types. One may have to depend on any one ofthe earlier described methods to decide on an appropriate basic datatype to be used as the array element type or structure member type.

The object of the invention is to provide a unique method ofimplementing fixed-width data types, which are not directly supported bya programming language, in a portable manner.

SUMMARY OF THE INVENTION:

The method according to the invention is described below in thefollowing steps.

A set U of required fixed-width data types that have to be implementedin which each fixed-width data type U_(k) from the set U has a fixeddata type width of WU_(k) is provided as input along with an ordered setB of basic data types that are directly supported by the saidprogramming language, in which each basic data type B_(i) from the set Bhas a data type width WB_(i) and each data type width WB_(i+1) isgreater than or equal to data type width WB_(i), and a set V having allpossible data type widths WV_(j) for every basic data type B_(i) fromthe set B of basic data types.

A generic data type G with two formal parameters consisting of aninteger parameter and a data type parameter is created.

For every combination of data type width WV_(j) from the set V, andbasic data type B_(i) from the set B, a specialized generic data type Ghaving an integer parameter WVj and a data type parameter B_(i) iscreated and a possible implementation within the specialized genericdata types G is provided for each required fixed-width data type U_(k)from the set U by comparing the data type width WV_(j) with data typewidth WU_(k) for every required fixed-width data types U_(k) from theset U.

If data type width WV_(j) is equal to the data type width WU_(k), therequired fixed-width data type U_(k) is implemented by creating andmapping data type U_(k) to data type B_(i). If data type width WV_(j) isgreater than the data type width WU_(k), the required fixed-width datatype U_(k) is implemented by using a sub-range of basic data type B_(i).If data type width WV_(j) is lesser than the data type width WU_(k), andif B_(i) is not the last basic data type form the set B, the requiredfixed-width data type U_(k) is implemented by mapping U_(k) to theimplementation of U_(k) provided by the specialized generic data type Ghaving the integer parameter WB_(i+1) and the data type parameterB_(i+1). If data type width WV_(j) is lesser than the data type widthWU_(k) and if B_(i) is the last basic data type from set B, the requiredfixed-width data type U_(k) is implemented by using an array, with theleast required number of elements of basic data type B_(i) or a record,with least required number of fields of basic data type B_(i).

Finally the set U of required fixed-width data types U_(k) isimplemented by selecting from the above possible implementations, acorrect implementation for each required fixed-width data type U_(k) ofthe set U of required fixed-width data types, by creating and mappingthe required fixed-width data type U_(k) to the implementation of U_(k)provided by the specialized generic data type G having the integerparameter WB₁ and the data type parameter B₁.

An alternate method within the scope of the invention for achieving thesame objective comprises the following steps.

A set U of required fixed-width data types that have to be implementedin which each fixed-width data type U_(k) from the set U has a fixeddata type width of WU_(k) is provided as input along with an ordered setB of basic data types that are directly supported by the saidprogramming language, in which each basic data type B_(i) from the set Bhas a data type width WB_(i) and each data type width WB_(i+1) isgreater than or equal to data type width WB_(i), and a set V having allpossible data type widths WV_(j) for every basic data type B_(i) fromthe set B of basic data types.

A generic data type G with two formal parameters namely an integerparameter and a data type parameter is created.

For every combination of data type width WV_(j) from the set V, andbasic data type B_(i) from the set B a specialized generic data typeG_(ji) having an integer parameter WVj and a data type parameter B_(i)is created and a possible implementation within the specialized genericdata types G_(ji) is provided for each required fixed-width data typeU_(k) from the set U by comparing the data type width WV_(j) with datatype width WU_(k) for every required fixed-width data types U_(k) fromthe set U.

If data type width WV_(j) is equal to the data type width WU_(k), therequired fixed-width data type U_(k) is implemented by creating andmapping data type U_(k) to data type B_(i). If data type width WV_(j) isgreater than the data type width WU_(k), and if B_(i) is not the firstbasic data type from the set B, the required fixed-width data type U_(k)is implemented by creating and mapping the required fixed-width datatype U_(k) to the implementation of U_(k) provided by the specializedgeneric data type G having the integer parameter WB_(i-1) and the datatype parameter B_(i-1). If data type width WV_(j) is greater than thedata type width WU_(k), and if B_(i) is the first basic data type formthe set B, the required fixed-width data type U_(k) is implemented byusing a sub-range of basic data type B_(i). If data type width WV_(j) islesser than the data type width WU_(k), the required fixed-width datatype U_(k) is implemented by using an array, with the least requirednumber of elements of basic data type B_(i) or a record, with leastrequired number of fields of basic data type B_(i).

Finally the set U of required fixed-width data types U_(k) isimplemented by selecting from the above possible implementations, acorrect implementation for each required fixed-width data type U_(k)from the set U of required fixed-width data types, by creating andmapping the required fixed-width data type U_(k) to the implementationof U_(k) provided by the specialized generic data type G having aninteger parameter WB_(n) and a data type parameter B_(n), where B_(n) isthe last basic data type from the set B of basic data types. Throughouthe above description, i, j, k′ and n are all positive integers.

BRIEF DESCRIPTION OF THE DRAWINGS

FIG. 1 is a schematic diagram of system with which the invention can beimplemented.

FIGS. 2 and 3 are flow charts of the method according to a particularembodiment of the invention.

DESCRIPTION WITH REFERENCE TO THE ACCOMPANYING DRAWINGS

The system shown in FIG. 1 may be used for implementing the invention.It comprises a memory (101), processing device (102) and a userinterface device (103). The memory (101) typically consists of RAM andROM. The processing device (102) may be a microprocessor. The userinterface may be key board, a mouse and a display. To implement aprogram, the program must be compiled using the processing device (102).As described herein, the processing device (102) implements fixed-widthdata types which are directly supported by a programming language in aportable manner satisfying the hardware constraints by the methodaccording to the invention.

FIG. 2 is a flow chart showing the main method steps taken to achievethe object of the invention.

In step 201 a set U of required fixed-width data types, an ordered set Bof basic data types and a set V with all possible data type widths arefed as inputs.

In step 202 a generic data type G having an integer parameter and a datatype parameter is created.

In step 203, for every combination of data type width WV_(j) from set V,and basic data type B_(i) from set B a specialized generic data type Ghaving an integer parameter WV_(j) and data type parameter data typeparameter B_(i) is created.

In step 204 each required fixed-width data type U_(k) is implemented bycreating and mapping U_(k) to the implementation provided by G₁₁ havinginteger type parameter WB₁ and data type parameter B₁.

FIG. 3 shows the details of the step 203 shown in FIG. 2.

In step 301 data type width WV_(j) from a set V having all possible datatype widths and basic data type B_(i) from an ordered set B of basicdata types are provided as inputs.

In step 302 specialized generic data type G_(ji) having an integerparameter WV_(j) and data type parameter B_(i) is created.

In step 303 data type width WV_(j) is compared with the width WU_(k) ofevery required fixed-width data type U_(k) from the set U.

In steps 304 and 307 data type U_(k) is implemented by mapping U_(k) toB_(i) if the data type width WV_(j) is equal to data type width WU_(k).

In steps 305 and 308 data type U_(k) is implemented using a sub-range ofbasic data type B_(i) if data type width WV_(j) is greater than datatype width WU_(k).

In steps 306 and 309, if basic data type B_(i) is not the last basicdata type B_(n) from set B, then data type U_(k) is mapped to theimplementation of U_(k) provided by G having integer parameter WB_(i+1)and data type parameter B_(i+1).

In steps 306 and 310 required fixed-width data type U_(k) is implementedby using an array of least number of elements of basic data type B_(i)or least number of fields of basic data type B_(i).

A typical example in which the method according to the invention is usedwith programming language C++ is given below.

Module Name: fixed_width_int.h # include <limits.h> // Inputs: // // SetB: char short long // //Set U: Char (Wchar = 8 bits) Short (Wshort = 16bits) long (Wlong = 32 bits) // // Set V: 8, 16, 32 // // ImplementsFixed-Width Integer Data Types // // Char 8-bits // Short 16-bits //Long 32-bits // // Get value of CHAR_BIT from limits.h // WB values#define Wchar CHAR_BIT #define Wshort (CHAR_BIT * sizeof (short)) #define Wlong (CHAR_BIT * sizeof (long)) // A template class declarationthat implements sub-ranges types. // Definition/Implementation notincluded in this sample. // // FP1 = width of basic type FP2 // FP3 =width of required integer type. Template <int FP1, class FP2, int FP3 >struct SUBRANGE; // // A template class declaration that implementsextensions using arrays. // Definition/Implementation not included inthis sample. // // FP1 = Width of basic type FP2 // FP3 = Width ofrequired integer type. Template < int. FP1, class FP2, int FP3> structextension; // // template class declaration for a general purposefixed-width // integer . (Implementation not part of this sample). //template <int FP1, class FP2, int FP3>struct FWINT; // A General Type,providing a possibly inefficient implementation of U template <int FP1,class FP2 >struct G { typedef FWINT<FP1, FP2, 8> Char; typedefFWINT<FP1, FP2 , 16> Short; typedef FWINT<FP1, FP2, 32> Long; }; //SpecializedGeneric Type G for 32; long template <> struct G<32, long> {typedef SUBRANGE<32, long, 8> Char; typedef SUBRANGE<32, long, 16>Short; typedef long Long; }; // Specialized Generic Type G for 16, longtemplate <> Struct G<16, long> { typedef SUBRANGE<16, long, 8> Char;typedef short Short; typedef EXTENSION<16, long, 32> Long; }; //Assuming long is *at least* 16 bits, there is no need for G<8, long> //Specialized Generic Type G for 32, short template <> struct G<32, short>{ typedef SUBRANGE<32, short, 8 >Char; typedef SUBRANGE<32, short, 16>Short; typedef short Long; }; // Specialized Generic Type G for 16,short template <> struct G<16, short> { typedef SUBRANGE<16, short,8 >Char; typedef short Short; typedef G<Wlong, long>: : Long Long; }; //Specialized Generic Type G for 8, short template <> struct G<8, short> {typedef short Char; typedef G<Wlong, long>: :Short Short; typedefG<Wlong, long>: : Long Long; }; // Specialized Generic Type G for 32,char template <> struct G<32, char> { typedef SUBRANGE<32, char,8 >Char; typedef SUBRANGE<32, char, 16 > Short; typedef char Long; }; //Specialized Generic Type G for 16, char template <> struct G<16, char >{ typedef SUBRANGE<16, char, 8 >Char; typedef char Short; typedefG<Wshort, short>: : Long Long; }; // Specialized Generic Type G for 8,char template <> struct G<8, char> { typedef char Char; typedefG<Wshort, short>: :Short Short; typedef G<Wshort, short>: : Long Long;}; // // Final Step: Create and map U1 (Char), U2 (Short), U3 (Long) //and map them to the implementation of corresponding type // provided byG<Wchar, char> // typedef G<Wchar, char>: :Char Char; typedef G<Wchar,char>: :Short Short; typedef G<Wchar, char>: :Long Long;

1. A method of implementing in a portable manner, fixed-width data typeswhere such fixed-width data types are not directly supported by aprogramming: language, said method comprising the steps of a) providingas inputs (i) a set U of required fixed-width data types that have to beimplemented in which each fixed-width data type U_(k) from the set U hasa fixed-data type width of WU_(k); (ii) an ordered set B of basic datatypes that are directly supported by the said programming language, inwhich each basic data type B_(i) from the set B has a data type widthWB_(i) and each data type width WB_(i+1) is greater than or equal todata type width WB_(i); and (iii) a set V having all possible data typewidths WV_(j) for every basic data type B_(i) from the set B of basicdata types; b) creating a generic data type G with two formal parametersconsisting of an integer parameter and a data type parameter; c) forevery combination of data type width WV_(j) from the set V, and basicdata type B_(i) from the set B creating a specialized generic data typeG_(ji) having an integer parameter WVj and a data type parameter B_(i)and providing a possible implementation within the specialized genericdata types G_(ji) for each required fixed-width data type U_(k) from theset U by comparing the data type width WV_(j) with data type widthWU_(k) for every required fixed width data types U_(k) from the set U;d).(i) if data type width WV_(j) is equal to the data type width WU_(k),implementing the required fixed-width data type U_(k) by creating andmapping data type U_(k) to data type B_(i); (ii) if data type widthWV_(j) is greater than the data type width WU_(k), implementing therequired fixed-width data type U_(k) by using a sub-range of basic datatype B_(i); (iii) if data type width WV_(j) is lesser than the data typewidth WU_(k) and if B_(i) is not the last basic data type form the setB, implementing the required fixed-width data type U_(k) by mappingU_(k) to the implementation of U_(k) provided by the specialized genericdata type G having the integer parameter WB_(i+1) and the data typeparameter B_(i+1); and (iv) if data type width WV_(j) is lesser than thedata type width WU_(k) and if B_(i) is the last basic data type from setB, implementing the required fixed-width data type U_(k) by using anarray with the least required number of elements of basic data typeB_(i) or a record with least required number of fields of basic datatype B_(i); and e) finally implementing the set U of requiredfixed-width data types U_(k) by selecting from the above possibleimplementations a correct implementation for each required fixed datatype U_(k) of the set U of required fixed-width data types, by creatingand mapping the required fixed-width data type U_(k) to theimplementation of U_(k) provided by the specialized generic data type Ghaving the integer parameter WB_(i) and the data type parameter B₁wherein i, j, k and n are all positive integers.
 2. A method ofimplementing in a portable manner, fixed-width data types where suchfixed-width data types are not directly supported by a programminglanguage, said method comprising the steps of: a) providing as inputs(i) a set U of required fixed-width data types that have to beimplemented in which each fixed-width data type U_(k) from the set U hasa fixed data type width of WU_(k); (ii) an ordered set B of basic datatypes that are directly supported by the said programming language, inwhich each basic data type B_(i) from the set B has a data type widthWB_(i) and each data type width WB_(i+1) is greater than or equal todata type width WB_(i); and (iii) a set V having all possible data typewidths WV_(j) for every basic data type B_(i) from the set B of basicdata types; b) creating a generic data type G with two formal parametersconsisting of an integer parameter and a data type parameter; c) forevery combination of data type width WV_(j) from the set V, and basicdata type B_(i) from the set B creating a specialized generic data typeG_(ji) having an integer parameter WVj and a data type parameter B_(i)and providing a possible implementation within the specialized genericdata types G_(ji) for each required fixed-width data type U_(k) from theset U by comparing the data type width WV_(j), with data type widthWU_(k) for every required fixed-width data types U_(k) from the set U;d).(i) if data type width WV_(j) is equal to the data type width WU_(k),implementing the required fixed-width data type U_(k) by creating andmapping data type U_(k) to data type B_(i); (ii) if data type widthWV_(j) is greater than the data type width WU_(k), and if B_(i) is notthe first basic data type from the set B, implementing the requiredfixed-width data type U_(k) by creating and mapping the requiredfixed-width data type U_(k) to the implementation of U_(k) provided bythe specialised generic data type G having the integer parameterWB_(i-1) and the data type parameter B_(i-1); (iii) if data type widthWV_(j) is greater than the data type width WU_(k) and if B_(i) is thefirst basic data type form the set B, implementing the required fixedwidth data type U_(k) by using a sub-range of basic data type B_(i); and(iv) if data type width WV_(j) is lesser than the data type widthWU_(k), implementing the required fixed-width data type U_(k) by usingan array, with the least required number of elements of basic data typeB_(i) or a record, with least required number of fields of basic datatype B_(i); and finally implementing the set U of required fixed-widthdata types U_(k) by selecting from the above possible implementations acorrect implementation for each required fixed-width data type U_(k)from the set U of required fixed-width data types, by creating andmapping the required fixed-width data type U_(k) to the implementationof U_(k) provided by the specialized generic data type G having integerparameter WB_(n) and the data type parameter B_(n), where B_(n) beingthe last basic data type from the set B of basic data types; wherein i,j, k and n are all positive integers.